package com.example.cnotebook.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.cnotebook.R;

import java.util.ArrayList;

public class AddFragment extends BaseFragment implements View.OnClickListener {
    private Spinner spCategory;
    private ImageView imSave;
    private EditText etTitle;
    private EditText etContent;
    private Context context = null;

    private ArrayList<String> categorys = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_add_layout, null);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        initData();
    }

    private void initData() {

    }

    private void initView(View view) {
        context = getContext();
        categorys.add("未选择");
        spCategory = view.findViewById(R.id.sp_category);
        imSave = view.findViewById(R.id.im_save);
        etTitle = view.findViewById(R.id.et_title);
        etContent = view.findViewById(R.id.et_content);

        ArrayAdapter<String> adapter = new ArrayAdapter<>(context, android.R.layout.simple_spinner_item, categorys);
        spCategory.setAdapter(adapter);
        spCategory.setSelection(0);

        imSave.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.im_save) {
            saveData();
        }
    }

    private void saveData() {
        String title = etTitle.getText().toString();
        String content = etContent.getText().toString();
        if (title.equals("") && content.equals("")) {
            Toast.makeText(context, "标题或者内容不能为空！", Toast.LENGTH_SHORT).show();
            return;
        }
        onDestroy();
    }
}
